package l1j.opqlo.自動發放;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Set;
import java.util.TimerTask;
import java.util.concurrent.ScheduledFuture;
import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.opqlo.Server.L1World.L1World;
import l1j.server.DatabaseFactory;
import l1j.server.Thread.PcOtherThreadPool;
import l1j.server.server.datatables.CharObjidTable;
import l1j.server.server.datatables.ItemTable;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.templates.L1Item;
import l1j.server.server.utils.SQLUtil;

public class 推文發放 extends TimerTask {

	private static final Logger _log = Logger.getLogger(推文發放.class.getName());

	private static HashMap<Integer , 推文物品> _pcList ;

	private static void resetGive(){
		Connection con = null;
		PreparedStatement pstm = null;
		try {
			con = DatabaseFactory.get().getConnection();
			pstm = con.prepareStatement("UPDATE Z_opqlo_推文發放 SET 已加載 = ?");
			pstm.setInt(1, 0);
			pstm.execute();
		} catch (final Exception e) {
			_log.log(Level.SEVERE, "ブックマークの削除でエラーが発生しました。", e);
		} finally {
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
	}

	private static void updateGive(final String name ,final int itemId){
		Connection con = null;
		PreparedStatement pstm = null;
		try {
			con = DatabaseFactory.get().getConnection();
			pstm = con.prepareStatement("UPDATE Z_opqlo_推文發放 SET 已加載 = ? where 玩家名稱=? && 獎勵編號 = ?");
			pstm.setInt(1, 1);
			pstm.setString(2, name);
			pstm.setInt(3, itemId);
			pstm.execute();
		} catch (final Exception e) {
			_log.log(Level.SEVERE, "ブックマークの削除でエラーが発生しました。", e);
		} finally {
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
	}


	private ScheduledFuture<?> _timer;

	public 推文發放(){
		_pcList = new HashMap<Integer , 推文物品>();
		resetGive();
		}

	private void loadDate(){
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try {
			con = DatabaseFactory.get().getConnection();
			pstm = con.prepareStatement("SELECT * FROM Z_opqlo_推文發放 where 已加載 = 0");
			rs = pstm.executeQuery();

			while (rs.next()) {
				final int itemid = rs.getInt("獎勵編號");
				final L1Item itemTemplate = ItemTable.getInstance().getTemplate(itemid);
				if (itemTemplate == null) {
					continue;
				}
				final int count = rs.getInt("獎勵數量");
				if(count <=0){
					continue;
				}
				final String name = rs.getString("玩家名稱");
				final int obj_id =CharObjidTable.get().charObjid(name);
				if(obj_id ==0){
					continue;
				}
				推文物品 temp = _pcList.get(obj_id);
				if(temp == null){
					temp = new 推文物品(obj_id);
					_pcList.put(obj_id, temp);
				}
				temp.addItem(itemid, count , rs.getInt("enchantlvl") , rs.getInt("bless") , rs.getInt("is_id"));
				updateGive(name ,itemid);
				
			}
		} catch (final Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(),e);
		} finally {
			SQLUtil.close(rs , pstm , con);
		}
	}

	@Override
	public void run() {
		this.loadDate();
		if(_pcList.isEmpty()){
			return;
		}
		try {
			final Set<Integer>keyList = _pcList.keySet();
			for(final int key : keyList){
				final L1PcInstance pc = (L1PcInstance) L1World.getInstance().findObject(key);
				// 不包含元素
				if (pc != null) {
					_pcList.get(key).start(pc);
				}
			}
		} catch (final Exception e) {
			_log.log(Level.SEVERE ,"推文自動發放時間軸異常重啟", e);
			PcOtherThreadPool.get().cancel(this._timer, false);
			final 推文發放 hprCrown = new 推文發放();
			hprCrown.start();
		}
	}

	public void start() {
		final int timeMillis = 60000;// 1秒
		this._timer = PcOtherThreadPool.get().scheduleAtFixedRate(this, timeMillis, timeMillis);
	}

}
